/*
 * @Author: 郝奔 1425466221@qq.com
 * @Date: 2024-11-29 10:20:27
 * @LastEditors: shiweizhen 787632169@qq.com
 * @LastEditTime: 2025-06-26 10:00:26
 * @FilePath: /HG-dqbasic_open_platform/vite.config.ts
 * @Description:
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved.
 */
import { UserConfig, ConfigEnv, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import path from 'path';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueSetupExtend from 'vite-plugin-vue-setup-extend-plus';
import DefineOptions from 'unplugin-vue-define-options/vite'; // 引入插件
import AutoImport from 'unplugin-auto-import/vite'; //自动导入
import Components from 'unplugin-vue-components/vite'; //自动导入
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'; //自动导入
// import eslintPlugin from "vite-plugin-eslint";
export default ({ mode }: ConfigEnv): UserConfig => {
  // 获取 .env 环境配置文件
  const env = loadEnv(mode, process.cwd());

  return {
    plugins: [
      vue(),
      DefineOptions(),
      createSvgIconsPlugin({
        iconDirs: [
          path.resolve(process.cwd(), 'src/assets/uploadSvg'),
          path.resolve(process.cwd(), 'src/assets/icons'),
          path.resolve(process.cwd(), 'src/framework/assets/icons'),
          path.resolve(
            process.cwd(),
            'src/framework/unified-navigation/assets/svg'
          )
        ],
        symbolId: 'icon-[dir]-[name]' // 指定symbolId格式
      }),
      // * EsLint 报错信息显示在浏览器界面上
      // eslintPlugin(),
      // * vite 可以使用 jsx/tsx 语法
      vueJsx(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        dts: 'types/auto-imports.d.ts',
        imports: ['vue', 'vue-router'],
        eslintrc: {
          enabled: false // 1、改为true用于生成eslint配置。2、生成后改回false，避免重复生成消耗
        }
      }),
      Components({
        dts: 'types/components.d.ts',
        resolvers: [ElementPlusResolver()]
      }),
      // * name 可以写在 script 标签上
      vueSetupExtend()
    ],
    // 本地反向代理解决浏览器跨域限制
    server: {
      host: '0.0.0.0',
      port: Number(env.VITE_APP_PORT),
      open: true, // 运行自动打开浏览器
      headers: {
        'Access-Control-Allow-Origin': '*'
      },
      proxy: {
        [env.VITE_APP_BASE_API]: {
          // 线上API地址
          target: env.VITE_APP_TARGET,
          // 本地API地址
          // target: 'http://localhost:8989',
          changeOrigin: true
        }
      }
    },
    resolve: {
      // Vite路径别名配置
      alias: {
        '@': path.resolve('./src')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          silenceDeprecations: ['legacy-js-api']
        }
      }
    }
  };
};
